<?php

/**
 *
 *
 */
class Datarow
{
	public $name = ''; //name of column in table
	public $col = ''; //name of column in db query
	public $link = ''; //should this col be a link? You can use replaces in link like {id} which is value of dbcol id
	public $static = false; //static field
	public $replaces = Array();
	/**
	 * Constructor
	 */
	function __construct($name,$col,$link,$static)
	{
		$this->name = $name;
		$this->col = $col;
		$this->link = $link;
		$this->static = $static;
	}

	/**
	 * Datarow::render()
	 * Render value for this column and do replaces for {} and []
	 * @param mixed $row
	 * @return
	 */
	public function render($row)
	{
		if($this->static)	$row[$this->col] = $this->col;

		$content = $this->replaces($this->col,$row);

		if(strlen($this->link) > 0)
		{
			$link = $this->replaces($this->link,$row);

			echo '<td><a href="'.$link.'">',$content,'</a></td>';
		}
		else
		{
			echo '<td>',$content,'</td>';
		}
	}

	/**
	 * Datarow::add_replace()
	 * Add replace, like 1 => OK 0 => Bad
	 * @param mixed $replace Array $find=>$replace
	 * @return
	 */
	public function add_replace($replace)
	{
		foreach ($replace as $find=>$rep)
		{
			$this->replaces[$find] = $rep;
		}
	}

	/**
	 * Datarow::replaces()
	 * Takes care of replacing {} and [] values and internal replaces
	 * @param mixed $in
	 * @param mixed $row
	 * @return
	 */
	private function replaces($in,$row)
	{
		global $core;

		//print_r($this->replaces);

		//replace values
		foreach ($this->replaces as $find=>$replace)
		{
			//echo "v $row[$in] hledam $find a nahrazuji za $replace<br />";
			$row[$in] = str_replace($find,$replace,$row[$in]);
		}

		$count = 0;
		preg_match_all('/.*?{(.*?)}.*?/',$in,$res); //normal replaces with {}
		if(sizeof($res) > 1) //there are replaces
		{
			$res = $res[1];
			foreach ($res as $rep) //loop through found {XXX}
			{
				$find[] = '{'.$rep.'}'; //find it
				$replace[] = $row[$rep]; //and replace it with value
				$count++;
			}

			$in = str_replace($find,$replace,$in); //do it!
		}

		preg_match_all('/.*?\[(.*?)\].*?/',$in,$res); //url encoded replaces with []
		if(sizeof($res) > 1) //there are replaces
		{
			$res = $res[1];
			foreach ($res as $rep) //loop through found [XXX]
			{
				$find[] = '['.$rep.']'; //find it
				$replace[] = $core->encode_url($row[$rep]); //and replace it with value
				$count++;
			}

			$in = str_replace($find,$replace,$in); //do it!
		}

		if($count == 0) return $row[$in];
		else return $in;
	}
}

/**
 *
 *
 */
class Datatable
{
	public $query = ''; //query for table data
	public $cols = Array();
	private $counter = 0;

	/**
	 * Constructor
	 */
	function __construct($query)
	{
		global $db;

		$this->query = $query;
		$this->db = $db;
	}

	/**
	 * Datatable::add_column()
	 * Add column to datatable
	 * @param mixed $name Name in table header
	 * @param mixed $col Name of column in db query
	 * @param string $link Optional link, {dbcol} will be replaced with that col value, [dbcol] do the same, but result is urlencoded
	 * @param bool $static If true, this col is static, content is stored in $col
	 * @return
	 */
	public function add_column($name,$col,$link = '',$static = false)
	{
		$obj = new Datarow($name,$col,$link,$static);
		$this->cols[$this->counter] = $obj;
		$this->counter++;
		return $this;
	}

	/**
	 * Datatable::add_replace()
	 * Add replace for some values
	 * @param mixed $find
	 * @param mixed $replace
	 * @return
	 */
	public function add_replace($replace)
	{
		$index = $this->counter - 1;
		$this->cols[$index]->add_replace($replace);
		return $this;
	}

	/**
	 * Datatable::delete_link()
	 * Delete link for rows
	 * @param mixed $name
	 * @param mixed $link
	 * @param string $image Standard Yaps! ico
	 * @return
	 */
	public function delete_link($name,$link,$image = '')
	{
		$this->delete_col = true;
		$this->delete_link = $link;
		$this->delete_name = $name;
		$this->delete_img = $image;

		$this->add_column(' ','delete_link',$link);
	}

	/**
	 * Datatable::edit_link()
	 * Edit link for rows
	 * @param mixed $name Alt for image
	 * @param mixed $link link
	 * @param string $image Standard Yaps! ico
	 * @return
	 */
	public function edit_link($name,$link,$image = '')
	{
		$this->edit_col = true;
		$this->edit_link = $link;
		$this->edit_name = $name;
		$this->edit_img = $image;

		$this->add_column(' ','edit_link',$link);
	}

	/**
	 * Datatable::render_header()
	 * Render header for table
	 * @return
	 */
	private function render_header()
	{
		global $theme;

		//create table header
		echo '<table class="nice">
						<tr>';

		for($i = 0; $i < sizeof($this->cols); $i++)
		{
			echo '<th>'.$this->cols[$i]->name.'</th>';
		}

		echo '</tr>';
	}

	/**
	 * Datatable::draw_table()
	 * Draw datatable from $this->query
	 * @return
	 */
	public function draw_table()
	{
		global $core,$theme;

		if(sizeof($this->cols) > 0)
		{
			$this->render_header();

			//lets write data
			$res = $this->db->get_rows($this->query);
			for($i = 0; $i < sizeof($res); $i++)
			{
				echo '<tr>';
				foreach ($this->cols as $key=>$col)
				{
					if($col->col == 'edit_link') $res[$i]['edit_link'] = ($this->edit_img) ? $theme->icon($this->edit_img,$this->edit_name,true) : $this->edit_name;
					elseif($col->col == 'delete_link') $res[$i]['delete_link'] = ($this->delete_img) ? $theme->icon($this->delete_img,$this->delete_name,true) : $this->delete_name;
					$col->render($res[$i]);
					//echo 'renderuji: ',print_r($col,true),'<br />';
				}
				echo '</tr>';
			}

			echo '</table>';
		}
		else
		{
			$core->err('This query - '.$this->query.' has no columns!');
		}
	}
}

?>